package se.topshelf.golftracker.util;

import se.topshelf.golftracker.core.domain.*;
import se.topshelf.golftracker.core.repository.BaseRepository;

/**
 * Utility class for tests that easily saves complex domain object graphs. For instance, if a MemberClub has an
 * address that lies in a country, the country must be saved before the MemberClub. The same goes for the Country's
 * Continent mapping. And so on. This saves tons of time when writing tests.
 */
public class ObjectPersister {

    private ObjectPersister() {
    }

    public static void persist(MemberClub memberClub, BaseRepository baseRepository) {
        persist(memberClub.getAddress().getCountry(), baseRepository);
        baseRepository.saveOrUpdate(memberClub);

    }

    public static void persist(Country country, BaseRepository baseRepository) {
        persist(country.getContinent(), baseRepository);
        baseRepository.saveOrUpdate(country);
    }

    private static void persist(Continent continent, BaseRepository baseRepository) {
        baseRepository.saveOrUpdate(continent);
    }

    public static void persist(Hole hole, BaseRepository baseRepository) {
        baseRepository.saveOrUpdate(hole);
    }

    public static void persist(Player player, BaseRepository baseRepository) {
        persist(player.getMemberClub(), baseRepository);
        persist(player.getHomeAddress().getCountry(), baseRepository);
        baseRepository.saveOrUpdate(player);
    }

    public static void persist(Course course, BaseRepository baseRepository) {
        persist(course.getMemberClub(), baseRepository);
        baseRepository.saveOrUpdate(course);
    }

    public static void persist(Club club, BaseRepository baseRepository) {
        persist(club.getClubModel(), baseRepository);
        baseRepository.saveOrUpdate(club);

    }

    public static void persist(ClubModel clubModel, BaseRepository baseRepository) {
        baseRepository.saveOrUpdate(clubModel.getManufacturer());
        baseRepository.saveOrUpdate(clubModel);

    }

    public static void persist(TeeTime teeTime, BaseRepository baseRepository) {
        persist(teeTime.getCourse(), baseRepository);
        baseRepository.saveOrUpdate(teeTime);
    }

    public static void persist(HoleResult holeResult, BaseRepository baseRepository) {
        persist(holeResult.getRound(), baseRepository);
        baseRepository.saveOrUpdate(holeResult);
    }

    private static void persist(Round round, BaseRepository baseRepository) {
        baseRepository.saveOrUpdate(round);

    }

    public static void persist(Shot shot, BaseRepository baseRepository) {
        persist(shot.getHoleResult(), baseRepository);
        persist(shot.getClub(), baseRepository);
        baseRepository.saveOrUpdate(shot);
    }
}
